package org.wattdepot.cli.command;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Writes out an html file displaying a chart representation of the power generated.
 * 
 * @author Alex Kan and Kelvin Green
 * @version 2.0
 */
public class WattDepotChartCommand extends WattDepotCommand {
  /**
   * Writes out an html file displaying a chart representation of the power generated.
   * 
   * @param args contains an array of strings created from the user input.
   * @return a string containing info about chart creation.
   */
  public String doCommand(String args[]) {
    // check for correct input
    if (args.length != 10 || 0 != args[6].compareTo("sampling-interval")
        || 0 != args[8].compareTo("file")) {
      return "Correct command is 2.10 chart power [generated|consumed] {source} {startday} "
          + "{endday} sampling-interval {minutes} file {file}";
    }
    return getChart(args);
  }

  /**
   * Writes out an html file displaying a chart representation of the power generated.
   * 
   * @param args contains an array of strings created from the user input.
   * @return a string variable named output containing all the output for the method.
   */
  private String getChart(String[] args) {

    XMLGregorianCalendar startTS = null;
    XMLGregorianCalendar endTS = null;
    String output = "";
    BufferedWriter out = null;
    boolean error = false;

    try {
      startTS = Tstamp.makeTimestamp(args[4]);
      endTS = Tstamp.makeTimestamp(args[5]);
    }
    catch (Exception e) {
      return "Invalid day entered.";
    }

    Integer interval = Integer.parseInt(args[7]);

    List<Double> powerList = new ArrayList<Double>();

    try {
      while (Tstamp.daysBetween(startTS, endTS) > 1) {

        powerList.add(getClient().getPowerGenerated(args[3], startTS) / 10000000);
        startTS = Tstamp.incrementMinutes(startTS, interval);
      }
      out = new BufferedWriter(new FileWriter(args[9]));
      out.write("<html><body><img src=http://chart.apis.google.com/"
          + "chart?chs=250x100&cht=lc&chd=t:");
      for (int i = 0; i < powerList.size(); i++) {

        if (i == 0) {
          output = "" + powerList.get(i);
        }
        else {
          output = "," + powerList.get(i);
        }
        out.write(output);
      }
      out.write("></body></html>");
      out.close();
    }
    catch (MiscClientException mce) {
      return CON_NOT_FOUND;
    }
    catch (NotAuthorizedException e) {
      error = true;
    }
    catch (ResourceNotFoundException e) {
      return RES_NOT_FOUND;
    }
    catch (BadXmlException e) {
      error = true;
    }
    catch (IOException e) {
      error = true;
      if (error) {
        e.printStackTrace();
        return "I/O Error occurred.";
      }
    }

    return "Data was successfully written to " + args[9];
  }

}